package cn.anecansaitin.whimsy.client.item.render;

import cn.anecansaitin.whimsy.Whimsy;
import cn.anecansaitin.whimsy.common.item.RockSceptre;
import com.mojang.blaze3d.platform.Lighting;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.math.Axis;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.entity.ItemRenderer;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.item.ItemStack;
import software.bernie.geckolib.model.DefaultedItemGeoModel;
import software.bernie.geckolib.renderer.GeoItemRenderer;

public class RockSceptreRender extends GeoItemRenderer<RockSceptre> {
    public static final ResourceLocation ICON = new ResourceLocation(Whimsy.MODID, "item/rock_sceptre_icon");

    public RockSceptreRender() {
        super(new DefaultedItemGeoModel<>(new ResourceLocation(Whimsy.MODID, "rock_sceptre")));
    }

    @Override
    public void renderByItem(ItemStack stack, ItemDisplayContext transformType, PoseStack poseStack, MultiBufferSource bufferSource, int packedLight, int packedOverlay) {
        if (transformType == ItemDisplayContext.GROUND) {
            renderOnGround(poseStack, bufferSource, packedLight, packedOverlay);
        } else if (transformType == ItemDisplayContext.FIXED) {
            renderWhenFixed(poseStack, bufferSource, packedLight, packedOverlay);
        } else {
            super.renderByItem(stack, transformType, poseStack, bufferSource, packedLight, packedOverlay);
        }
    }

    @Override
    protected void renderInGui(ItemDisplayContext transformType, PoseStack poseStack, MultiBufferSource bufferSource, int packedLight, int packedOverlay) {
        poseStack.pushPose();
        Lighting.setupForFlatItems();
        renderIcon(poseStack, bufferSource, packedLight, packedOverlay);
        RenderSystem.disableDepthTest();
        ((MultiBufferSource.BufferSource) bufferSource).endBatch();
        RenderSystem.enableDepthTest();
        Lighting.setupFor3DItems();
        poseStack.popPose();
    }

    protected void renderOnGround(PoseStack poseStack, MultiBufferSource bufferSource, int packedLight, int packedOverlay) {
        poseStack.pushPose();
        poseStack.scale(0.5f, 0.5f, 0.5f);
        poseStack.translate(0.5, 0.5, 0.5);
        renderIcon(poseStack, bufferSource, packedLight, packedOverlay);
        poseStack.popPose();
    }

    private void renderWhenFixed(PoseStack poseStack, MultiBufferSource bufferSource, int packedLight, int packedOverlay) {
        poseStack.pushPose();
        poseStack.rotateAround(Axis.YP.rotationDegrees(180), 0.5f, 0f, 0.5f);
        renderIcon(poseStack, bufferSource, packedLight, packedOverlay);
        poseStack.popPose();
    }

    private void renderIcon(PoseStack poseStack, MultiBufferSource bufferSource, int packedLight, int packedOverlay) {
        Minecraft mc = Minecraft.getInstance();
        ItemRenderer itemRenderer = mc.getItemRenderer();
        BakedModel bakedModel = itemRenderer.getItemModelShaper().getModelManager().getModel(ICON);

        for (var model : bakedModel.getRenderPasses(ItemStack.EMPTY, true)) {
            for (var rendertype : model.getRenderTypes(ItemStack.EMPTY, true)) {
                VertexConsumer vertexconsumer = ItemRenderer.getFoilBufferDirect(bufferSource, rendertype, true, false);
                itemRenderer.renderModelLists(model, ItemStack.EMPTY, packedLight, packedOverlay, poseStack, vertexconsumer);
            }
        }
    }
}
